//回顾实现自己的ArrayList
class MyArrayList {
    //使用int[]去表达
    private int[] arr;
    private int size;//表示有效的个数

    public MyArrayList(int speace) {
        arr = new int[speace];
    }

    public int size() {
        return size;
    }

    //新增元素 , 从尾部增加
    public void add(int val) {
        if (size == arr.length) {
            rearr();
        }
        arr[size] = val;
        size++;
    }

    public void rearr() {
        int[] newarr = new int[(int) size + size / 2];
        for (int i = 0; i < arr.length; i++) {
            newarr[i] = arr[i];
        }
        arr = newarr;
        //在java中代表arra使用newarr新的数组；旧的数组就会被回收
    }

    //从任意位置增加
    public void add(int index, int val) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("IndexOutofBoundsException");
        }
        if (size == arr.length) {
            rearr();
        }
        for (int i = index; i < size; i++) {
            arr[i + 1] = arr[i];
        }
        arr[index] = val;
        size++;
    }

    //获取指定下标元素
    public int get(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException(+index + "超出有范围");
        }
        return arr[index];
    }

    //根据下标设置元素
    public void set(int index, int val) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException(+index + "超出有范围");
        }
        arr[index] = val;
    }

    //移除指定下标的元素
    public int remove(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException(index + "超出范围");
        }
        int sum = arr[index];//记录该下标的元素
        for (int i = index; i < size - 1; i++) {
            arr[i] = arr[i + 1];
        }
        size--;
        return sum;
    }

    //清空元素
    public void clear() {
        //直接去使用size来控制，就不需要将数组中的每个元素设为0;
        size = 0;

    }

    //找到了就返回该元素的下标，没找到就返回-1；
    public int index(int val) {
        for (int i = 0; i < size; i++) {
            if (arr[i] == val) {
                return i;
            }
        }
        return -1;
    }

    //判断元素是否该存在
    public boolean contain(int val) {
        for (int i = 0; i < size; i++) {
            if (arr[i] == val) {
                return true;
            }
        }
        return false;
    }

    //打印所有元素
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < size; i++) {
            stringBuilder.append(arr[i]);
            if (i < size - 1) {
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}

   public class Test1{
    //一些测试方法
    public static void test(){
        MyArrayList myArrayList=new MyArrayList(10);
        myArrayList.add(1);
        myArrayList.add(1);
        myArrayList.add(1);
        myArrayList.add(1);
        myArrayList.add(1);
        myArrayList.add(2);
        myArrayList.add(2);
        myArrayList.add(2);
        myArrayList.add(2);
        myArrayList.add(2);
        myArrayList.add(3);
        myArrayList.add(3);
        myArrayList.set(0,8);
        System.out.println(myArrayList.size());
        System.out.println(myArrayList);
        System.out.println(myArrayList.get(1));
    }
    public static void test1(){
        MyArrayList list=new MyArrayList(5);
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(0,88);
        list.add(1,99);
        System.out.println(list);
    }
    public static void test2(){
        MyArrayList list1=new MyArrayList(5);
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.remove(2);
        int n= list1.index(2);
        list1.clear();
        System.out.println(list1);
        System.out.println(n);
    }
    public static void test3(){
        MyArrayList list2=new MyArrayList(10);
        list2.add(1);
        list2.add(2);
        if(list2.contain(2)){
            System.out.println("ok");
        }
        else System.out.println("failed");
    }
    public static void main(String[] args) {
        test();
        test1();
        test2();
        test3();
    }
}

